#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setCentralWidget(ui->MainSplitter);
    //*****************************************主窗口自适应
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(ui->LogicSplitter);
    ui->Logic_widget->setLayout(layout);

    //*****************************************串口端口自适应
    layout = new QVBoxLayout;
    layout->addWidget(ui->Serial_Widget);
    ui->Serial_Tab->setLayout(layout);
    //*****************************************图象自适应
    layout = new QVBoxLayout;
    layout->addWidget(ui->Wave_Widget);
    ui->Wave_Tab->setLayout(layout);

    //*****************************************搜索串口
    serial = new MySerial;
    QList<QString> a = serial->get_SystemPort();
    for(int i = 0 ; i < a.size() ; i++)
    {
        ui->COM_Box->addItem(a.at(i));
    }

    connect(serial,&MySerial::getRev,ui->Serial_Widget,&SerialForm::RevSerialDat);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_SerialCtrl_Btn_released()
{
    if(ui->SerialCtrl_Btn->text() == "打开串口")
    {
        QMap<QString,QString> map;
        map["COM"]     = ui->COM_Box->currentText();
        map["Baud"]    = ui->Baud_Box->currentText();
        map["Stop"]    = ui->Stop_Box->currentText();
        map["DataBit"] = ui->DataBit_Box->currentText();
        map["Verify"]  = ui->Verify_Box->currentText();
        if(serial->Serial_Open(map)) //串口打开成功
        {
            ui->SerialCtrl_Btn->setText("关闭串口");
            serial_thread = new SerialThread(this);
            serial_thread->setPort(serial);
            connect(serial_thread,&SerialThread::finished,serial_thread,&SerialThread::deleteLater);
            serial_thread->start();
            connect(ui->Serial_Widget,&SerialForm::SendSerialDat,serial,&MySerial::SendData);
        }
        else
        {
            QMessageBox::question(this,"提示","串口打开失败",
                                  QMessageBox::Yes,
                                  QMessageBox::Yes);
        }
    }
    else
    {
        ui->SerialCtrl_Btn->setText("打开串口");
        serial->Serial_Close();
        serial_thread->setRun(0);
        disconnect(ui->Serial_Widget,&SerialForm::SendSerialDat,serial,&MySerial::SendData);
    }

}
